/*
 * CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
 * 
 * Copyright (C) 2007 Fernando M.A.d.S. <fermads@gmail.com>
 *
 * Contributors :
 *
 * Michael Hurni <michael.hurni@gmail.com>
 * Takuya Otani <takuya.otani@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU Lesser General Public License as published by the Free Software Foundation.
 * 
 * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
 */

// [note] CodePress supports basic syntax highlight on WebKit, at this stage.

CodePress = {
	scrolling : false,
	autocomplete : true,
	initialize : function()
	{
		if (typeof(editor) == 'undefined' && !arguments[0]) return;
		chars = ''; // charcodes that trigger syntax highlighting
		cc = '\u2009'; // control char
		editor = document.getElementsByTagName('body')[0];
		completeChars = this.getCompleteChars();
		document.designMode = 'on';
		document.addEventListener('keypress', this.keyHandler, false);
	},
	keyHandler : function(evt)
	{
		keyCode = evt.keyCode;
		charCode = evt.charCode;
		if( chars.indexOf('|'+charCode+'|') != -1 )
		{
			window.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
		}
		else if (keyCode == 13)
		{
			CodePress.onEnterkey(evt);
		}
		else if (keyCode==9 || evt.tabKey)
		{
			CodePress.onTabkey(evt);
		}
		else if (charCode == 118 && evt.ctrlKey)
		{ // handle paste
			window.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
		}
	},
	onTabkey : function(evt)
	{
		window.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
		evt.preventDefault();
	},
	onEnterkey : function(evt)
	{
		// do nothing
	},
	getRange : function()
	{
		var selection = window.getSelection();
		if (selection.getRangeAt) return selection.getRangeAt(0);
		var range = window.document.createRange();
		range.setStart(selection.baseNode, selection.baseOffset);
		range.setEnd(selection.extentNode, selection.extentOffset);
		return range;
	},
	findString : function()
	{
		// do nothing
	},
	split : function(code,flag)
	{
		// do nothing
	},
	syntaxHighlight : function(flag)
	{
		var text = editor.innerHTML;
		text = text.replace(/<div>/ig,'\n');
		text = text.replace(/<br.*?>/ig,'\n');
		text = text.replace(/<.*?>/g,'');
		text = text.replace(/\t/g,'<kbd>\t</kbd>');
		text = text.replace(/\n/g,'<br>');
		
		for(i=0;i<Language.syntax.length;i++) 
			text = text.replace(Language.syntax[i].input,Language.syntax[i].output);
		
		editor.innerHTML = text;
	},
	getLastWord : function()
	{
		// do nothing
	},
	snippets : function(evt)
	{
		// do nothing
	},
	readOnly : function()
	{
		document.designMode = (arguments[0]) ? 'off' : 'on';
	},
	complete : function(trigger)
	{
		// do nothing
	},
	getCompleteChars : function()
	{
		// do nothing
	},
	shortcuts : function()
	{
		// do nothing
	},
	getRangeAndCaret : function()
	{
		// do nothing
	},
	insertCode : function(code,replaceCursorBefore)
	{
		var range = CodePress.getRange();
		var node = window.document.createTextNode(code);
		range.deleteContents();
		range.insertNode(node);
	},
	getCode : function()
	{
		var code = editor.innerHTML;
		code = code.replace(/<div>/ig,'\n');
		code = code.replace(/<br.*?>/ig,'\n');
		code = code.replace(/\u2009/g,'');
		code = code.replace(/<.*?>/g,'');
		code = code.replace(/&lt;/g,'<');
		code = code.replace(/&gt;/g,'>');
		code = code.replace(/&amp;/gi,'&');
		return code;
	},
	setCode : function()
	{
		var code = arguments[0];
		code = code.replace(/\u2009/gi,'');
		code = code.replace(/&/gi,'&amp;');
		code = code.replace(/</g,'&lt;');
		code = code.replace(/>/g,'&gt;');
		editor.innerHTML = code;
	},
	actions : {
		pos : -1, // actual history position
		history : [], // history vector
		undo : function() {
		},
		redo : function() {
		},
		next : function() {
		}
	}
};

Language = {};
window.addEventListener('load', function() { CodePress.initialize('new'); }, true);
